package com.byteslounge.websockets;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint("/websocket")
public class WebSocketTest {

    @OnMessage
    public void onMessage(String message, Session session)
            throws IOException, InterruptedException {

        SessionCache.put(message, session.getId());
    }

    @OnOpen
    public void onOpen(Session session) {
        SessionCache.put(session.getId(), session);
        System.out.println("Client connected : " + session.getId());
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("Connection closed : " + session.getId());
    }

    @OnError
    public void onError(Throwable t, Session session) throws IOException {
        SessionCache.remove(session.getId());
        System.out.println("中断了一个连接, sesionID : " + session.getId());
    }
}
